<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item label="昵称">
        <el-input v-model="queryObj.and.like.nickname" placeholder="昵称" clearable></el-input>
      </el-form-item>
      <el-form-item label="账号状态">
        <el-select v-model="queryObj.and.must.status">
          <el-option label="全部" value=""></el-option>
          <el-option label="正常" value="1"></el-option>
          <el-option label="禁用" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="账号类型">
        <el-select v-model="queryObj.and.must.official">
          <el-option label="全部" value=""></el-option>
          <el-option label="官方账号" value="1"></el-option>
          <el-option label="普通账号" value="0"></el-option>
        </el-select>
      </el-form-item>
       <el-form-item label="禁言状态">
        <el-select v-model="queryObj.and.must.official">
          <el-option label="全部" value=""></el-option>
          <el-option label="禁言" value="1"></el-option>
          <el-option label="正常" value="0"></el-option>
        </el-select>
      </el-form-item>
    <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <!-- <el-button  type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('forum:member:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button> -->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="ID"
        width="200">
      </el-table-column>
      <el-table-column
        prop="username"
        header-align="center"
        align="center"
        label="账号"
        width="200">
      </el-table-column>
      <el-table-column
        prop="nickname"
        header-align="center"
        align="center"
        label="昵称"
        width="150">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        label="头像">
        <template slot-scope="scope">
          <img :src="scope.row.avatar" width="50px">
        </template>
      </el-table-column>
      <el-table-column
        prop="phone"
        header-align="center"
        align="center"
        label="手机号"
        width="130">
      </el-table-column>
      <el-table-column
        prop="sex"
        header-align="center"
        align="center"
        label="性别"
        width="70">
        <template slot-scope="scope">
          <span v-if="scope.row.sex == 0">女</span>
          <span v-if="scope.row.sex == 1">男</span>
          <span v-else>未知</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="birthday"
        header-align="center"
        align="center"
        label="生日"
        width="150">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="账号状态"
        width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == '3'" size="small" type="danger">禁用</el-tag>
          <el-tag v-if="scope.row.status == '2'" size="small" type="warning">注销</el-tag>
          <el-tag v-if="scope.row.status == '1'" size="small">正常</el-tag>
        </template>
      </el-table-column>
       <el-table-column
        prop="official"
        header-align="center"
        align="center"
        label="官方账号"
        width="100">
         <template slot-scope="scope">
          <el-tag v-if="scope.row.official == 1">官方账号</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        label="禁言时间"
        width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.nospeak" size="small" type="danger" v-text="nospeakTime(scope.row.nospeak)"></el-tag>
          <el-tag v-else size="small">正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间"
        width="200">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="250"
        label="操作">
        <template slot-scope="scope">
           <el-button type="text" size="small" @click="normalSpeak(scope.row)"  v-if="scope.row.nospeak">解除禁言</el-button>
           <el-button type="text" size="small" @click="nospeakDialog(scope.row.id,scope.row.nickname)" v-if="!scope.row.nospeak">禁言</el-button>
           <el-button type="text" size="small" @click="disableOrActivate(scope.row)" v-if="scope.row.status == 1">禁用账号</el-button>
           <el-button type="text" size="small" @click="disableOrActivate(scope.row)" v-if="scope.row.status == 3">解除禁用</el-button>
           <el-button type="text" size="small" @click="setOrReleaseOfficial(scope.row)" v-if="scope.row.official == 0">设置为官方账号</el-button>
           <el-button type="text" size="small" @click="setOrReleaseOfficial(scope.row)" v-if="scope.row.official == 1">设置为普通账号</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>

    <el-dialog :title="'禁言'+nickname" :visible.sync="nospeakVisible">
      <el-form :model="nospeakObj" ref="nospeakRef" label-position="top" :rules="dataRule">
        <el-form-item label="时长">
          <el-input-number v-model="nospeakObj.timeNum" :min="1" :max="10000" label="时长"></el-input-number>
        </el-form-item>
         <el-form-item label="单位">
          <el-radio v-model="nospeakObj.timeUnit" label="1">分</el-radio>
          <el-radio v-model="nospeakObj.timeUnit" label="2">时</el-radio>
          <el-radio v-model="nospeakObj.timeUnit" label="3">天</el-radio>
          <el-radio v-model="nospeakObj.timeUnit" label="4">月</el-radio>
          <el-radio v-model="nospeakObj.timeUnit" label="5">年</el-radio>
        </el-form-item>
        <el-form-item label="禁言理由" prop="reason">
          <el-input v-model="nospeakObj.reason" ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="nospeakVisible = false">取 消</el-button>
        <el-button type="primary" @click="nospeak">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import AddOrUpdate from './member-add-or-update'
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        nickname:'',
        timeUnit:["分","时","天","月","年"],
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        messageVisible:false,
        nospeakVisible:false,
        queryObj: {
          pageParams: {
            page: 1,  //当前页
            limit:10,//每页记录数
            sidx: "create_time",
            order: "asc",
          },
          and: {//放在and里面的都是 并且条件
            must:{status:'',official:''},
            like: {nickname:''}, //模糊匹配
            must:{}, //精确匹配
            ge:{'create_time':''}, //大于 xxx值
            le:{'create_time':''}, //小于 xxx值
            between: [ //范围匹配（注意，时间条件不要用范围查询，效果不好）
              ['sort','','']
            ],
          }
        },
        nospeakObj:{
          memberId:'',
          timeNum:'0',
          timeUnit:'1',
          reason:''
        },
        dataRule: {
          reason: [
            { required: true, message: '请填写理由', trigger: 'blur' }
          ]
        }
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/forum/member/multiConditMemberVoList'),
          method: 'post',
          data: this.$http.adornParams(this.queryObj)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      sizeChangeHandle (val) {
        this.queryObj.pageParams.limit = val
        this.queryObj.pageParams.page = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.queryObj.pageParams.page = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      nospeakTime(nospeak){
        if(nospeak.timeUnit == 4){
          return nospeak.timeNum+"个"+this.timeUnit[nospeak.timeUnit-1];
        }
        return nospeak.timeNum+this.timeUnit[nospeak.timeUnit-1];
      },
      //打开禁言窗口
      nospeakDialog(id,nickname){
        this.nospeakVisible= true;
        this.nickname=nickname;
        this.nospeakObj={memberId:id,timeNum:'0',timeUnit:'1',reason:''}
      },

      //禁言
      nospeak(){
        this.$refs['nospeakRef'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl("/forum/member/nospeak"),
              method: 'post',
              data: this.$http.adornData(this.nospeakObj)
            }).then(({data}) => {
              this.operationTips(data,this.getDataList);
            })
          }
        })
      },
      normalSpeak(member){
        this.$confirm(`确定解除[${member.nickname}]的禁言吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
              url: this.$http.adornUrl(`/forum/member/normalSpeak/${member.id}`),
              method: 'post',
              data: this.$http.adornData()
            }).then(({data}) => {
              this.operationTips(data,this.getDataList);
            })
        })
      },
      update(member){
        this.$http({
              url: this.$http.adornUrl(`/forum/member/update`),
              method: 'post',
              data: this.$http.adornData(member)
            }).then(({data}) => {
              this.operationTips(data,this.getDataList);
            })
      },
      //禁用或者激活账号
      disableOrActivate(member){
        let newStatus = member.status==1?0:1;
        this.$confirm(`确定${newStatus==0?'禁用':'激活'}[${member.nickname}]吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            member.status = newStatus;
            this.update(member)
        })
      },
      //设置或解除官方账号
      setOrReleaseOfficial(member){
        let newOfficial = member.official==1?0:1;
        this.$confirm(`确定将[${member.nickname}]${newOfficial==0?'设置普通账号':'设置为官方账号'}吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            member.official= newOfficial;
            this.update(member)
        })
      },
      operationTips(data,successCallback){
        if (data && data.code === 0) {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.nospeakVisible = false
            }
          })
          successCallback();
        } else {
          this.$message.error(data.msg)
        }
      }
    }
  }
</script>
